package com.caszx.chargingpile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONException;
import com.app.base.app.BaseTopBarDelayActivity;
import com.app.utils.DiskUser;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.eventbus.CommentBus;
import com.caszx.chargingpile.http.ConstantUrl;
import com.caszx.chargingpile.utils.SnackbarHelp;

import org.json.JSONObject;

import java.util.HashMap;

import de.greenrobot.event.EventBus;
import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import okhttp3.Call;

/**
 * 评价
 * */
public class CommentActivity extends BaseTopBarDelayActivity implements OnClickListener {


    private EditText edit_content ;
    private TextView txt_state ;
    private String ordersId ;
    private int degreeMarke;

    @Override
    protected void init() {
        ordersId = getIntent().getStringExtra("ID");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        setCenterTitle("评价");
    }

    @Override
    protected void findView() {

        findViewById(R.id.emoji_01).setOnClickListener(this);
        findViewById(R.id.emoji_02).setOnClickListener(this);
        findViewById(R.id.emoji_03).setOnClickListener(this);
        findViewById(R.id.emoji_04).setOnClickListener(this);
        findViewById(R.id.emoji_05).setOnClickListener(this);
        findViewById(R.id.btn_post).setOnClickListener(this);
        edit_content = (EditText) findViewById(R.id.edit_content);
        txt_state = (TextView) findViewById(R.id.txt_state);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.emoji_01:
                txt_state.setText("差");
                degreeMarke = 1;
                break;
            case R.id.emoji_02:
                txt_state.setText("一般");
                degreeMarke = 2;
                break;
            case R.id.emoji_03:
                txt_state.setText("满意");
                degreeMarke = 3;
                break;
            case R.id.emoji_04:
                txt_state.setText("很满意");
                degreeMarke = 4;
                break;
            case R.id.emoji_05:
                txt_state.setText("强烈推荐");
                degreeMarke = 5;
                break;
            case R.id.btn_post:

                String content = edit_content.getText().toString().trim();
                if(TextUtils.isEmpty(content)){
                    SnackbarHelp.showSnackbarMsg(this,"最少输入15个字");
                    return;
                }

                if(content.length()<15){
                    SnackbarHelp.showSnackbarMsg(this,"最少输入15个字");
                    return;
                }
                comment(content,degreeMarke);
                break;

        }

    }

    private void comment(String content,int degreeMarke){
        showProgressDialog("提交中");
        HashMap<String,String> params = new HashMap<>();
        params.put("token", DiskUser.getStringValueByKey(this,"token"));
        params.put("ordersId",ordersId);
        params.put("content",content);
        params.put("degreeMarke",degreeMarke+"");

        OkHttpUtils.post()
                .url(ConstantUrl.URL_COMMENT_CHARGE)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        dissmissProgressDialog();
                        // showErrorView(R.string.error_network);
                        SnackbarHelp.showSnackbarMsg(CommentActivity.this,getString(R.string.error_network));
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        dissmissProgressDialog();
                        JSONObject resultJsonObject =  response.optJSONObject("result");
                        if(resultJsonObject!=null){
                            int code =  resultJsonObject.optInt("code");
                            if(code<0){
                                //error
                                // DialogHelp.showDialogOnlyMesg(resultJsonObject.optString("msg"),null);
                                //showErrorView(resultJsonObject.optString("msg"));
                                SnackbarHelp.showSnackbarMsg(CommentActivity.this,resultJsonObject.optString("msg"));
                            }else{
                                try {
                                    //SnackbarHelp.showSnackbarMsg(CommentActivity.this,resultJsonObject.optString("msg"));
                                    EventBus.getDefault().post(new CommentBus(ordersId));
                                    finish();
                                }catch (JSONException e){
                                    SnackbarHelp.showSnackbarMsg(CommentActivity.this,getString(R.string.error_parser_json));
                                    //showErrorView(R.string.error_parser_json);
                                }
                            }
                        }
                    }
                });
    }

}
